﻿using EYWeb.Core;
using EYWeb.Domain.Entities;
using EYWeb.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYWeb.Services
{
    public class ModuleService : BaseService<ModuleEntity>, IModuleService
    {
        private readonly IModuleRepository _moduleRepository;
        private readonly IRoleAuthorizeRepository _roleAuthorizeRepository;
        public ModuleService(IModuleRepository moduleRepository, IRoleAuthorizeRepository roleAuthorizeRepository) : base(moduleRepository)
        {
            _moduleRepository = moduleRepository;
            _roleAuthorizeRepository = roleAuthorizeRepository;
        }


        public List<ModuleEntity> GetMenuList(string roleId)
        {
            var data = new List<ModuleEntity>();
            if (OperatorProvider.Provider.GetCurrent().IsSystem)
            {
                data = _moduleRepository.FindAll().ToList();
            }
            else
            {
                var module = _moduleRepository.FindAll().OrderBy(t => t.SortCode).ToList();
                var authorize = _roleAuthorizeRepository.FindListByClause(t => t.ObjectId == roleId && t.ItemType == 1);
                foreach (var item in authorize)
                {
                    ModuleEntity entity = module.Find(t => t.Id == item.ItemId);
                    if (entity != null)
                    {
                        data.Add(entity);
                    }
                }
            }
            return data.OrderBy(t => t.SortCode).ToList();


        }

    }
}
